package test;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-05
 * Time:19:47
 */
public class GameHallPage extends Utils {

    private static final int MAX_REPLAY = 3;
    private  int tryCount = 0;
    public static String url = "http://81.70.248.108:58081/game_hall.html";
    public GameHallPage() {
        super(url);
    }

    /**
     * 检查游戏大厅页面页面元素
     */
    public void checkGameHall() throws InterruptedException {
//        Thread.sleep(1000);
        //检查用户信息是否加载
        webDriver.findElement(By.cssSelector("#screen"));
        //开始匹配按钮
        String button = webDriver.findElement(By.cssSelector("#match-button")).getText();
        //获取页面标题
        String title = webDriver.getTitle();
        assert title.equals("游戏大厅");
        assert button.equals("开始匹配");

    }
    public GameRoomPage match() throws InterruptedException {
        By matchButtonLocator = By.cssSelector("#match-button");
        //开始匹配按钮
        String initButton = webDriver.findElement(matchButtonLocator).getText();
        //获取页面标题
        String originalTitle = webDriver.getTitle();
        //查看开始匹配按钮，并检查跳转界面是否正确
        //等待页面跳转
        WebDriverWait buttonWait = new WebDriverWait(webDriver,Duration.ofSeconds(3));
        buttonWait.until(ExpectedConditions.elementToBeClickable(matchButtonLocator));
        webDriver.findElement(matchButtonLocator).click();
        try{
            //进入游戏房间
            WebDriverWait titleWait = new WebDriverWait(webDriver,Duration.ofSeconds(3));
            titleWait.until(ExpectedConditions.titleIs("游戏房间"));
            System.out.println("进入游戏房间");
            return new GameRoomPage(false);
        }catch (TimeoutException e){
            System.out.println("等待界面跳转超时");
            //页面为跳转，取消匹配，再次点击匹配
            String nowButton = webDriver.findElement(matchButtonLocator).getText();
            System.out.println(nowButton);
            if (nowButton.equals("匹配中...(点击停止)")){
                System.out.println("第"+(tryCount+1)+"次取消匹配");
                //等待按钮可点击
                WebDriverWait buttonWait1= new WebDriverWait(webDriver,Duration.ofSeconds(3));
                buttonWait.until(ExpectedConditions.elementToBeClickable(matchButtonLocator));
                webDriver.findElement(matchButtonLocator).click();

                //等待按钮文本变化
                WebDriverWait buttonWait2 = new WebDriverWait(webDriver,Duration.ofSeconds(3));
                buttonWait2.until(ExpectedConditions.textToBePresentInElementLocated(matchButtonLocator,"开始匹配"));
                System.out.println("已停止匹配");
                //获取按钮文本
                String button2 = webDriver.findElement(matchButtonLocator).getText();
                System.out.println(button2);
                assert button2.equals("开始匹配"):"文本匹配失败";
                //尝试重新匹配
                if(tryCount < MAX_REPLAY){
                    System.out.println("第"+(tryCount+1)+"次开始匹配");
                    tryCount++;
                    return match();
                }else {
                    System.out.println("达到最大次数");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("匹配过程中发生未知错误", e);
        }
        throw new RuntimeException("匹配流程失败，无法进入游戏房间");
    }
    public void unLogin(){
        WebDriverWait alertWait = new WebDriverWait(webDriver,Duration.ofSeconds(3));
        alertWait.until(ExpectedConditions.alertIsPresent());
        //切换到弹窗
        Alert alert = webDriver.switchTo().alert();
        alert.dismiss();

    }
}

